package com.bingqing.service;



import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bingqing.dao.AuditDao;
import com.bingqing.dao.TypeDao;
import com.bingqing.entity.*;
import com.bingqing.exception.CustomException;
import com.bingqing.utils.JwtTokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AdminService
 * Package: com.bingqing.springboot.service
 * Description:
 *
 * @Author liuyujie
 * @Create 2024-03-06 13:46
 * @Version 1.0
 */
@Service
public class AuditService {
    @Resource
    AuditDao auditDao;

    @Resource
    TypeDao typeDao;

    public void add(Audit audit) {
        auditDao.insertSelective(audit);
    }

    public void update(Audit audit) {
        auditDao.updateByPrimaryKeySelective(audit);
    }

    public void deleteById(Integer id) {
        auditDao.deleteByPrimaryKey(id);
    }


    public PageInfo<Audit> findBySearch(Params params) {
        Admin user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isNull(user)){
            throw new CustomException("从token中未解析到用户信息，请重新登录");
        }
        //如果是student,只能查看自己的请假信息
        if("ROLE_STUDENT".equals(user.getRole())){
            params.setUserId(user.getId());
        }
        //params.setUserId(user.getId());
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Audit> list = auditDao.findBySearch(params);
        //1.判断list是否为空
        if(CollUtil.isEmpty(list)){
            return PageInfo.of(new ArrayList<>());
        }
        return PageInfo.of(list);
    }
}
